package com.bootdo.mall_part.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.UserDO;
import javafx.beans.value.ObservableObjectValue;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.mall_part.domain.MallEvaluate;
import com.bootdo.mall_part.service.MallEvaluateService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author lujingqian
 * @email 2100272728@qq.com
 * @date 2019-11-12 16:18:51
 */
 
@Controller
@RequestMapping("/mall_part/mallEvaluate")
public class MallEvaluateController extends BaseController{


	@Autowired
	private MallEvaluateService mallEvaluateService;
	
	@ModelAttribute
	public MallEvaluate get(@RequestParam(required=false) Long id) {
		if (id != null){
			return mallEvaluateService.get(id);
		}else{
			return new MallEvaluate();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/mall_part/mallEvaluate','mall_part:mallEvaluate:mallEvaluate',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','mall_part:mallEvaluate:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("mall_part:mallEvaluate:mallEvaluate")	
	String MallEvaluate(MallEvaluate mallEvaluate,HttpServletRequest request,HttpServletResponse response){
	    return "mall_part/mallEvaluate/mallEvaluate";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("mall_part:mallEvaluate:mallEvaluate")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据

		UserDO user = ShiroUtils.getUser();
		if (user == null) {
			return new PageUtils(new ArrayList<>(), 0);
		}
		if (user.getUserId() != 1) {
			params.put("merchantId", user.getUserId());
		}
		params.put("parentId", "0");
        Query query = new Query(params);
		List<MallEvaluate> mallEvaluateList = mallEvaluateService.list(query);
		int total = mallEvaluateService.count(query);
		PageUtils pageUtils = new PageUtils(mallEvaluateList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("mall_part:mallEvaluate:edit")
	String add(MallEvaluate mallEvaluate,HttpServletRequest request,HttpServletResponse response){
	    return "mall_part/mallEvaluate/mallEvaluateedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(MallEvaluate mallEvaluate) {
		return R.ok().put("i",mallEvaluate);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("mall_part:mallEvaluate:edit")
	public R save( MallEvaluate mallEvaluate){
	
		if(mallEvaluateService.save(mallEvaluate)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("mall_part:mallEvaluate:edit")
	public R remove( Long id){
		if(mallEvaluateService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("mall_part:mallEvaluate:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		mallEvaluateService.batchRemove(ids);
		return R.ok();
	}

	@PostMapping( "/getUnreadCount")
	@ResponseBody
	@RequiresPermissions("mall_part:mallEvaluate:edit")
	public R getUnread(){
		Map<String, Object> param = new HashMap<>();

		param.put("merchantId", ShiroUtils.getUserId());
		param.put("isRead", 1);
		param.put("replierId", ShiroUtils.getUserId());

		int total = mallEvaluateService.count(param);
		return R.ok().put("total", total);
	}

	@PostMapping( "/getUnreadEvaluate")
	@ResponseBody
	@RequiresPermissions("mall_part:mallEvaluate:edit")
	public R getUnreadEvaluate(Map<String, Object> param){

		param.put("merchantId", ShiroUtils.getUserId());
		param.put("isRead", 1);
		 param.put("replierId", ShiroUtils.getUserId());

		List<MallEvaluate> total = mallEvaluateService.list(param);
		Set<Long> idList = new HashSet<>();
		for (MallEvaluate one: total) {
			if (one.getParentId() == 0) {
				idList.add(one.getId());
			} else {
				idList.add(one.getGrandFatherId());
			}
		}
		Integer limit = (Integer) param.get("limit");
		Integer offset = (Integer) param.get("offset");
		if (limit == null) {
			limit = 10;
		}
		if (offset == null) {
			offset = 0;
		}

		List<MallEvaluate> unreadFather = mallEvaluateService.getUnreadFather(idList, offset, limit);
		int totalNumber = mallEvaluateService.getUnreadFatherCount(idList);


		return R.ok().put("rows", unreadFather).put("total", totalNumber);
	}

	@PostMapping( "/checkOneEvaluateDetail")
	@ResponseBody
	@RequiresPermissions("mall_part:mallEvaluate:edit")
	public R checkOneEvaluateDetail(@RequestParam("id")Long id){

		MallEvaluate evaluate = mallEvaluateService.get(id);
		List<MallEvaluate> mallEvaluates = mallEvaluateService.getByFatherId(id);
		mallEvaluates.add(0, evaluate);
		MallEvaluate endEvaluate = mallEvaluates.get(mallEvaluates.size() - 1);
		mallEvaluateService.setReaded(id, endEvaluate.getId(), ShiroUtils.getUserId());

		return R.ok().put("l", mallEvaluates);
	}
}